package com.ruoyi.map.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.map.service.MapService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 地图服务实现类
 * 
 * @author ruoyi
 */
@Service
public class MapServiceImpl implements MapService
{
    @Value("${amap.key:bdb7c94eedad49009c63220ea1f3907f}")
    private String amapKey;
    
    @Value("${amap.version:1.4.15}")
    private String amapVersion;
    
    @Value("${amap.base-url:https://restapi.amap.com/v3}")
    private String amapBaseUrl;
    
    private final RestTemplate restTemplate = new RestTemplate();

    @Override
    public Map<String, Object> getMapConfig()
    {
        Map<String, Object> config = new HashMap<>();
        config.put("key", amapKey);
        config.put("version", amapVersion);
        config.put("plugin", "AMap.Geocoder,AMap.PlaceSearch,AMap.AutoComplete");
        return config;
    }

    @Override
    public Map<String, Object> geocode(String address)
    {
        try
        {
            String url = String.format("%s/geocode/geo?key=%s&address=%s", 
                amapBaseUrl, amapKey, address);
            String response = restTemplate.getForObject(url, String.class);
            return JSON.parseObject(response, Map.class);
        }
        catch (Exception e)
        {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "0");
            error.put("info", "地理编码失败: " + e.getMessage());
            return error;
        }
    }

    @Override
    public Map<String, Object> regeocode(String location)
    {
        try
        {
            String url = String.format("%s/geocode/regeo?key=%s&location=%s", 
                amapBaseUrl, amapKey, location);
            String response = restTemplate.getForObject(url, String.class);
            return JSON.parseObject(response, Map.class);
        }
        catch (Exception e)
        {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "0");
            error.put("info", "逆地理编码失败: " + e.getMessage());
            return error;
        }
    }

    @Override
    public Map<String, Object> searchPoi(String keywords, String location, Integer radius)
    {
        try
        {
            StringBuilder url = new StringBuilder();
            url.append(amapBaseUrl).append("/place/around?key=").append(amapKey);
            url.append("&keywords=").append(keywords);
            if (location != null && !location.isEmpty())
            {
                url.append("&location=").append(location);
            }
            url.append("&radius=").append(radius);
            
            String response = restTemplate.getForObject(url.toString(), String.class);
            return JSON.parseObject(response, Map.class);
        }
        catch (Exception e)
        {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "0");
            error.put("info", "POI搜索失败: " + e.getMessage());
            return error;
        }
    }

    @Override
    public Map<String, Object> searchPoiNationwide(String keywords, String city)
    {
        try
        {
            StringBuilder url = new StringBuilder();
            url.append(amapBaseUrl).append("/place/text?key=").append(amapKey);
            url.append("&keywords=").append(keywords);
            if (city != null && !city.isEmpty())
            {
                url.append("&city=").append(city);
            }
            url.append("&offset=20"); // 返回结果数量
            url.append("&page=1");    // 页码
            
            String response = restTemplate.getForObject(url.toString(), String.class);
            return JSON.parseObject(response, Map.class);
        }
        catch (Exception e)
        {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "0");
            error.put("info", "全国POI搜索失败: " + e.getMessage());
            return error;
        }
    }
}
